# Purpose: Produce some figures related to the direction of Syrian->German friending

source("constants.R")

responses_and_friends <- read_csv("input/survey_analysis_input.csv")

responses_and_friends <- responses_and_friends %>% mutate(answered_any = (
    !is.na(satisfaction_numeric) +
    !is.na(job_finding_numeric) +
    !is.na(school_work_numeric) +
    !is.na(bureaucracy_numeric) +
    !is.na(healthcare_numeric) +
    !is.na(residence_numeric) +
    !is.na(courses_numeric) +
    !is.na(been_invited_home_numeric) +
    !is.na(invited_home_numeric) +
    !is.na(restaurant_numeric) +
    !is.na(greeted_numeric) +
    !is.na(sports_numeric) +
    !is.na(integration_numeric)) > 0)


##################################################
### 1. Make the summary stats for this sample ####
##################################################

survey_sample_summary_stats <- responses_and_friends %>%
    filter(answered_any) %>%
    summarise(
        `N Local Native Friends` = mean(n_frnd_nat_lcl, na.rm=T),
        `N Local Syrian Friends` = mean(n_frnd_sy_lcl, na.rm=T),
        `Produces Any DE Content (0/100)` = mean(produ_any_de, na.rm=T),
        `N Local Native Groups` = mean(n_lcl_50n_grps, na.rm=T),
        Age = mean(age, na.rm=T),
        `Is Female (0/100)` = mean(is_female * 100, na.rm=T),
        `DE College (0/100)` = mean(college_de, na.rm=T),
        `N Friends` = mean(total_30d_frnds, na.rm=T),
        `N Groups` = mean(total_grps, na.rm=T),
        `Qs Since 1st on FB in DE` = mean(quarters_since_de, na.rm=T),
        n=n(),
        n_finished = sum(finished)
    )

write_csv(survey_sample_summary_stats, "output/syrian_sample_summary_SURVEY.csv")


#######################################################
### 2. Table for Validation of Integration Measure ####
#######################################################

linear_controls = c("indiv_time_cntrl", "indiv_l90_cntrl", "indiv_l1080_cntrl", "ln_other_frnd", "ln_other_grps", "ln_cont_prod")
fes = c("curr_nuts3", "quarters_since_de", "is_female", "prev_qs_nuts3", "age_brkt_25_34", "age_brkt_35_44", "age_brkt_45_54", "age_brkt_55_plus")

# Small helper function to add the Y-Mean
model_y_mean <- function(model){ mean(model$residuals + model$fitted.values) }

reg_dat <- responses_and_friends %>%
    mutate(`N Local Native Friends` = n_frnd_nat_lcl_winsorized_p99)

validation_table_w_controls_regs <- list(
    been_invited_home1 <- fixest::feols(as.formula("been_invited_home_numeric ~ `N Local Native Friends`"),
                                        data=reg_dat, fixef.rm = "both"),
    been_invited_home2 <- fixest::feols(as.formula(str_interp("been_invited_home_numeric ~ `N Local Native Friends` + ${paste(linear_controls, collapse=' + ')} | ${paste(fes, collapse=' + ')}")),
                                        data=reg_dat, fixef.rm = "both"),
    invited_home1 <- fixest::feols(as.formula("invited_home_numeric ~ `N Local Native Friends`"),
                                        data=reg_dat, fixef.rm = "both"),
    invited_home2 <- fixest::feols(as.formula(str_interp("invited_home_numeric ~ `N Local Native Friends` + ${paste(linear_controls, collapse=' + ')} | ${paste(fes, collapse=' + ')}")),
                                        data=reg_dat, fixef.rm = "both"),
    restaurant1 <- fixest::feols(as.formula("restaurant_numeric ~ `N Local Native Friends`"),
                                        data=reg_dat, fixef.rm = "both"),
    restaurant2 <- fixest::feols(as.formula(str_interp("restaurant_numeric ~ `N Local Native Friends` + ${paste(linear_controls, collapse=' + ')} | ${paste(fes, collapse=' + ')}")),
                                        data=reg_dat, fixef.rm = "both"),
    sports1 <- fixest::feols(as.formula("sports_numeric ~ `N Local Native Friends`"),
                                        data=reg_dat, fixef.rm = "both"),
    sports2 <- fixest::feols(as.formula(str_interp("sports_numeric ~ `N Local Native Friends` + ${paste(linear_controls, collapse=' + ')} | ${paste(fes, collapse=' + ')}")),
                                        data=reg_dat, fixef.rm = "both"),
    integration1 <- fixest::feols(as.formula("integration_numeric ~ `N Local Native Friends`"),
                                        data=reg_dat, fixef.rm = "both"),
    integration2 <- fixest::feols(as.formula(str_interp("integration_numeric ~ `N Local Native Friends` + ${paste(linear_controls, collapse=' + ')} | ${paste(fes, collapse=' + ')}")),
                                        data=reg_dat, fixef.rm = "both"))

y_means <- sapply(validation_table_w_controls_regs, model_y_mean)

validation_table_w_controls <- etable(validation_table_w_controls_regs,
            headers = c(
                        "Been Invited to Native Home",
                        "Been Invited to Native Home",
                        "Invited Native to Own Home",
                        "Invited Native to Own Home",
                        "Visited Restaurant with Native",
                        "Visited Restaurant with Native",
                        "Played Sports with Native",
                        "Played Sports with Native",
                        "Freq. of Native Social Interactions",
                        "Freq. of Native Social Interactions"),
            signif.code = c("***"=0.01, "**"=0.05, "*"=0.10),
            extralines=list("_Y-Mean"=y_means))

names(validation_table_w_controls)[1] <- "_"
validation_table_w_controls <- validation_table_w_controls %>% as_tibble()
write_csv(validation_table_w_controls, "output/survey_validation_table_w_controls.csv")


################################
### 3. Outcomes Table Result ###
################################

outcome_table_w_controls_regs <- list(
    job_finding1 <- fixest::feols(as.formula("job_finding_numeric ~ `N Local Native Friends`"),
                                        data=reg_dat, fixef.rm = "both"),
    job_finding2 <- fixest::feols(as.formula(str_interp("job_finding_numeric ~ `N Local Native Friends` + ${paste(linear_controls, collapse=' + ')} | ${paste(fes, collapse=' + ')}")),
                                        data=reg_dat, fixef.rm = "both"),
    residence1 <- fixest::feols(as.formula("residence_numeric ~ `N Local Native Friends`"),
                                        data=reg_dat, fixef.rm = "both"),
    residence2 <- fixest::feols(as.formula(str_interp("residence_numeric ~ `N Local Native Friends` + ${paste(linear_controls, collapse=' + ')} | ${paste(fes, collapse=' + ')}")),
                                        data=reg_dat, fixef.rm = "both"),
    school_work1 <- fixest::feols(as.formula("school_work_numeric ~ `N Local Native Friends`"),
                                        data=reg_dat, fixef.rm = "both"),
    school_work2 <- fixest::feols(as.formula(str_interp("school_work_numeric ~ `N Local Native Friends` + ${paste(linear_controls, collapse=' + ')} | ${paste(fes, collapse=' + ')}")),
                                        data=reg_dat, fixef.rm = "both"),
    bureaucracy1 <- fixest::feols(as.formula("bureaucracy_numeric ~ `N Local Native Friends`"),
                                        data=reg_dat, fixef.rm = "both"),
    bureaucracy2 <- fixest::feols(as.formula(str_interp("bureaucracy_numeric ~ `N Local Native Friends` + ${paste(linear_controls, collapse=' + ')} | ${paste(fes, collapse=' + ')}")),
                                        data=reg_dat, fixef.rm = "both"),
    healthcare1 <- fixest::feols(as.formula("healthcare_numeric ~ `N Local Native Friends`"),
                                        data=reg_dat, fixef.rm = "both"),
    healthcare2 <- fixest::feols(as.formula(str_interp("healthcare_numeric ~ `N Local Native Friends` + ${paste(linear_controls, collapse=' + ')} | ${paste(fes, collapse=' + ')}")),
                                        data=reg_dat, fixef.rm = "both"),
    satisfaction1 <- fixest::feols(as.formula("satisfaction_numeric ~ `N Local Native Friends`"),
                                        data=reg_dat, fixef.rm = "both"),
    satisfaction2 <- fixest::feols(as.formula(str_interp("satisfaction_numeric ~ `N Local Native Friends` + ${paste(linear_controls, collapse=' + ')} | ${paste(fes, collapse=' + ')}")),
                                        data=reg_dat, fixef.rm = "both"))

y_means <- sapply(outcome_table_w_controls_regs, model_y_mean)

outcome_table_w_controls <- etable(outcome_table_w_controls_regs,
            headers = c(
                        "Native Friend Helped Find Job",
                        "Native Friend Helped Find Job",
                        "Native Friend Helped Finding Residence",
                        "Native Friend Helped Finding Residence",
                        "Native Friend Helped w/ School",
                        "Native Friend Helped w/ School",
                        "Native Friend Helped w/ Bureaucracy",
                        "Native Friend Helped w/ Bureaucracy",
                        "Native Friend Helped w/ Healthcare",
                        "Native Friend Helped w/ Healthcare",
                        "Life Satisfaction",
                        "Life Satisfaction"),
            signif.code = c("***"=0.01, "**"=0.05, "*"=0.10),
            extralines=list("_Y-Mean"=y_means))

names(outcome_table_w_controls)[1] <- "_"
outcome_table_w_controls <- outcome_table_w_controls %>% as_tibble()
write_csv(outcome_table_w_controls, "output/survey_outcome_table_w_controls.csv")
